package com.soho.controller;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;

import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Record;
import com.soho.core.Mt4;
import com.soho.helper.ContentHelper;
import com.soho.helper.PrintHelper;
import com.soho.helper.QuoteHelper;
import com.soho.helper.SqlHelper;
import com.soho.helper.StatusHelper;
import com.soho.helper.TimeHelper;
import com.soho.helper.UtilsHelper;
import com.soho.model.Symbols;

public class ApiController extends Controller {
	
	public void index() {
		renderText("Hello Would");
	}
	
	//获取货币列表
	public void getSymbolList() {
		UtilsHelper.cors(getResponse());
		renderJson(new Record().set("Symbols",Symbols.getList()));
	}
	
	//货币行情数据
	public void quoteHistory() {
		UtilsHelper.cors(getResponse());
		
		Kv kv=this.getKv();
		String uid=kv.getStr("Account");
		
		//后面这4个做查询使用,到时记得检验
		String symbol=kv.getStr("Symbol").replace(".", "");
		long from=Long.parseLong(kv.getStr("From"));
		int count=Integer.valueOf(kv.getStr("Count"));
		int timeFrame=Integer.valueOf(kv.getStr("TimeFrame"));
		//https://charting-library.tradingview.com/ 根据返回的时间进行判断
		LocalDateTime fromTime =LocalDateTime.ofInstant(Instant.ofEpochSecond(from),ZoneId.of(ContentHelper.ZONE_DEFAULT));  //东2区
		
		//先判断数据库中是否满足
		String tableName	=	StatusHelper.getTableFromTimeFrame(timeFrame);
//		
		System.out.println("Default Time = "+fromTime);
		//LocalDateTime	plusFromTime	=	fromTime.plusSeconds(18);//检查好像相差15s
		
		
		//2019.11.14 发送数据都会缺一条数据,比如远程10.25的数据,而拉取时只有拉到10.24的数据....
		String searchTime	=	StatusHelper.getSearchTimeFromFrame(fromTime, timeFrame, symbol);
		
		System.out.println("fromTime = "+fromTime+" AND Search Time = "+searchTime);
		
		List<Record> outRecord	= new ArrayList<>();
		
		List<Record> dbList		=	SqlHelper.getQuotesList(tableName, symbol,searchTime, count);
		if(dbList.size()==0) {
			List<Record>	tmpOutRecord	=	Mt4.getInstance().historyQuotes(symbol, StatusHelper.getFrame(timeFrame), fromTime, count);
			outRecord						=	tmpOutRecord;
		}else {
			
			if(dbList.get(0).getStr("Time").equals(searchTime) && dbList.size()>100) {
				//pass 相等的话,说明有数据,则不需要更新
				System.out.println("Read in Cache!");
				dbList.forEach((row)->{
					row.set("Time", TimeHelper.getSecondFromStr(row.getStr("Time")));
				});
				outRecord		=	dbList;
			}else {
				//如果不满足则调用接口
				List<Record> tmpOutRecord		=	Mt4.getInstance().historyQuotes(symbol, StatusHelper.getFrame(timeFrame), fromTime, count);
				outRecord						=	tmpOutRecord;
			}
		}
		renderJson(JsonKit.toJson(new Record().set("Bars",outRecord)));
	}
	
	//货币交易:市场价交易
	public void orderMarket() {
		renderText("orderMarket");
	}
	
	//取消订单
	public void orderCancel() {
		renderText("orderCancel");
	}
	
	//挂单交易
	public void orderPending() {
		renderText("orderPending");
	}
	
	//开始监听及发送信息
	public void startListen() {
		PrintHelper.print("Start Listen");
		QuoteHelper.startListen();
		renderText("Start Listen");
	}
	
	//监听指定货币
	public void startListenerSymbol() {
		String symbol=this.getPara("Symbol");
		if(UtilsHelper.checkIsEmpty(symbol)) {
			renderText("参数不能空");
		}
		Mt4.getInstance().subscribe(symbol);
		renderText("已发送订阅"+symbol);
	}
	
	//停止订阅指定货币
	public void stopListenerSymbol() {
		String symbol=this.getPara("Symbol");
		if(UtilsHelper.checkIsEmpty(symbol)) {
			renderText("参数不能空");
		}
		Mt4.getInstance().unSubscribe(symbol);
		renderText("已发送停止订阅"+symbol);
	}
}



